//
//  MTAgreementView.swift
//  MT
//
//  Created by ooops on 2020/3/21.
//  Copyright © 2020 张强. All rights reserved.
//

import Foundation
import MTCategory

protocol MTAgreementViewDelegate: NSObjectProtocol {
    func dismiss(isCancel: Bool)
    
    func userAgreement()
    
    func privacy()
}

class MTAgreementView: UIView {
    
    weak var delegate: MTAgreementViewDelegate?

    private lazy var titleLabel: UILabel = {
        let _titleLabel: UILabel = UILabel(frame: CGRect.zero)
        _titleLabel.font = UIFont.systemFont(ofSize: 12.0, weight: .regular)
        _titleLabel.text = "为了更好地维护您的利益，我们对《用户协议》及《隐私政策》进行了更新，特向您推送本提示，请仔细阅读并充分理解相关条款，特别是其中的隐私政策条款"
        _titleLabel.textColor = UIColor.init(hex: 0x474747)
        _titleLabel.textAlignment = .left
        _titleLabel.numberOfLines = 0
        return _titleLabel
    }()
    
    private lazy var richTitleLabel: YYLabel = {
        let _richTitleLabel: YYLabel = YYLabel(frame: CGRect.zero)
        _richTitleLabel.font = UIFont.systemFont(ofSize: 12.0, weight: .medium)
        _richTitleLabel.textAlignment = .left
        _richTitleLabel.numberOfLines = 0
        return _richTitleLabel
    }()
    
    private lazy var agreeButton: UIButton = {
        let _agreeButton: UIButton = UIButton(frame: CGRect.zero)
        _agreeButton.setTitle("我已阅读并同意上述条款", for: UIControl.State.normal)
        _agreeButton.setImage(UIImage.init(named: "home_question_option_default"), for: UIControl.State.normal)
        _agreeButton.setImage(UIImage.init(named: "btn_choose"), for: UIControl.State.selected)
        _agreeButton.setTitleColor(UIColor.init(hex: 0x474747), for: UIControl.State.normal)
        _agreeButton.addTarget(self, action: #selector(agree(sender:)), for: UIControl.Event.touchUpInside)
        _agreeButton.titleLabel?.font = UIFont.systemFont(ofSize: 12.0)
        _agreeButton.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
        _agreeButton.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 14)
        _agreeButton.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 14)
        return _agreeButton
    }()
    
    private lazy var confirmButton: UIButton = {
        let _confirmButton: UIButton = UIButton(frame: CGRect.zero)
        _confirmButton.clipsToBounds = true
        _confirmButton.isEnabled = false
        _confirmButton.backgroundColor = UIColor.init(hex: 0xA5A4A4)
        _confirmButton.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
        _confirmButton.layer.cornerRadius = 20.0
        _confirmButton.setTitle("确定", for: .normal)
        _confirmButton.setTitleColor(UIColor.white, for: .normal)
        _confirmButton.addTarget(self, action: #selector(didConfirmButtonClick), for: .touchUpInside)
        return _confirmButton
    }()
    
    private lazy var cancelButton: UIButton = {
        let _confirmButton: UIButton = UIButton(frame: CGRect.zero)
        _confirmButton.clipsToBounds = true
        _confirmButton.backgroundColor = UIColor.init(hex: 0xFFFFFF)
        _confirmButton.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
        _confirmButton.layer.cornerRadius = 20.0
        _confirmButton.setTitle("取消", for: .normal)
        _confirmButton.setTitleColor(UIColor.black, for: .normal)
        _confirmButton.addTarget(self, action: #selector(didCancelButtonClick), for: .touchUpInside)
        return _confirmButton
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(self.titleLabel)
        self.addSubview(self.richTitleLabel)
        self.addSubview(self.agreeButton)
        self.addSubview(self.confirmButton)
        self.addSubview(self.cancelButton)
        
        self.setupViewConstraints()
        
        self.setRichLabel()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupViewConstraints() {
        let kMargin: CGFloat = 20.0
        let kTopMargin: CGFloat = 10.0

        self.titleLabel.snp.makeConstraints({ (make) in
            make.left.equalTo(self).offset(kMargin)
            make.right.equalTo(self).offset(-kMargin)
            make.top.equalTo(self).offset(30)
            make.height.equalTo(60)
        })
        
        self.richTitleLabel.snp.makeConstraints({ (make) in
            make.top.equalTo(self.titleLabel.snp.bottom).offset(kTopMargin)
            make.left.right.equalTo(self.titleLabel)
            make.height.equalTo(80)
        })
        
        self.agreeButton.snp.makeConstraints({ (make) in
            make.top.equalTo(self.richTitleLabel.snp.bottom).offset(kTopMargin)
            make.left.right.equalTo(self.titleLabel)
            make.centerX.equalToSuperview()
            make.height.equalTo(40)
        })
        
        self.confirmButton.snp.makeConstraints({ (make) in
            make.width.equalTo(132)
            make.height.equalTo(40)
            make.left.equalTo(self.snp.centerX).offset(5)
            make.top.equalTo(self.agreeButton.snp.bottom).offset(kMargin)
        })
        
        self.cancelButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.confirmButton)
            make.right.equalTo(self.snp.centerX).offset(-5)
            make.width.height.equalTo(self.confirmButton)
        }
    }
    
    @objc func didConfirmButtonClick() {
//        DDCUserDefaults.setBool(true, forKey: "kConfirmUserAgreement")
        if self.confirmButton.isUserInteractionEnabled {
            self.delegate?.dismiss(isCancel: false)
        }
    }
    
    @objc func didCancelButtonClick() {
    //        DDCUserDefaults.setBool(true, forKey: "kConfirmUserAgreement")
            self.delegate?.dismiss(isCancel: true)
        }
    
    @objc func agree(sender: UIButton) {
        sender.isSelected = !sender.isSelected
        self.confirmButton.isEnabled = sender.isSelected
        self.confirmButton.isUserInteractionEnabled = sender.isSelected
        if sender.isSelected { //选中
            self.confirmButton.backgroundColor = UIColor.init(hex: 0xFF4F58)
        } else {
            self.confirmButton.backgroundColor = UIColor.init(hex: 0xA5A4A4)
        }
    }
    
    func setRichLabel() {
        //设置整段字符串的颜色
        let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key(rawValue: "NSFontAttributeName"): UIFont.systemFont(ofSize: 12.0, weight: .medium), NSAttributedString.Key(rawValue: "NSForegroundColorAttributeName"): UIColor.init(hex: 0x474747)]
        
        let attributedText: NSMutableAttributedString = NSMutableAttributedString.init(string: "您点击“同意”，即表示您已阅读并同意更新后的《用户协议》及《隐私政策》条款。", attributes: attributes)
        //设置高亮色和点击事件
        attributedText.setTextHighlight(attributedText.string.nsrange(of: "《用户协议》")[0], color: UIColor.init(hex: 0xFF4F58), backgroundColor: nil, tapAction: { (containerView, text, range, rect) in
//            let ecommController: DDCEcommerceWebViewController = DDCEcommerceWebViewController.init(urlString: String.init(format: "%@%@", (DDCStore.shared()?.base_h5_url)!,userAgreement), title: "用户协议")
//            self.viewController?.navigationController?.pushViewController(ecommController, animated: true)
            self.delegate?.userAgreement()
        }, longPressAction: { (view, text, range, rect) in
            
        })
        //        //设置高亮色和点击事件
        attributedText.setTextHighlight(attributedText.string.nsrange(of: "《隐私政策》")[0], color: UIColor.init(hex: 0xFF4F58), backgroundColor: nil, tapAction: { (containerView, text, range, rect) in
//            let ecommController: DDCEcommerceWebViewController = DDCEcommerceWebViewController.init(urlString:        String.init(format: "%@%@", (DDCStore.shared()?.base_h5_url)!,privacyPolicy) , title: "隐私政策")
//            self.viewController?.navigationController?.pushViewController(ecommController, animated: true)
            self.delegate?.privacy()
        }, longPressAction: { (view, text, range, rect) in
            
        })
        
        attributedText.setUnderlineStyle(.single, range: attributedText.string.nsrange(of: "《用户协议》")[0])
        attributedText.setUnderlineStyle(.single, range: attributedText.string.nsrange(of: "《隐私政策》")[0])
        
        attributedText.setUnderlineColor(.red, range: attributedText.string.nsrange(of: "《用户协议》")[0])
        attributedText.setUnderlineColor(.red, range: attributedText.string.nsrange(of: "《隐私政策》")[0])
        
        self.richTitleLabel.attributedText = attributedText
    }
}
